<style type="text/css">
.width-long{width:400px;}
</style>
<span id="get_data_remote_msg"></span>
<form method="post" id="form_product_edit" enctype="multipart/form-data" action="admin.php?ctrl=product&act=edit_submit">
      <table class="itable itable-color">
      <tr id="show_pid" style="display:<{if $product.pid}>table-row<{else}>none<{/if}>;">
          <td>商品id</td>
          <td>
          <input value="<{$product.pid}>" name="pid" readonly="true"/>
          </td>
          <td></td>
        </tr>
	<tr>
          <td>源商品id</td>
          <td>
          <input type="text" value="<{$product.source_id}>" name="source_id" id="source_id"/>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <input type="button" value="获取" id="get_data_remote"/>
          </td>
          <td></td>
        </tr>
        
        <tr>
          <td>商品来源</td>
          <td>
<select name="tid[]" id="source">
      	<{if $sources}>
      		<{foreach from=$sources item=t}>
      			<option value="<{$t.tid}>" <{if $t.tid==$source}>selected<{/if}> ><{$t.name}></option>
      		<{/foreach}>
      	<{/if}>
      </select></td>
          <td></td>
        </tr>
        
        
        
        <tr>
          <td>标题</td>
          <td><input type="text" value="<{$product.title}>" name="title" class="width-long"/></td>
          <td></td>
        </tr>
<tr>
          <td>描述</td>
          <td><textarea name="description"><{$product.description}></textarea></td>
          <td></td>
        </tr>
<tr>
          <td>商家</td>
          <td><input type="text" value="<{$product.nick}>" name="nick"/></td>
          <td></td>
        </tr>
<tr>
          <td>销量</td>
          <td><input type="text" value="<{if $product.sales}><{$product.sales}><{else}>0<{/if}>" name="sales"/></td>
          <td></td>
        </tr>
<tr>
          <td>链接地址</td>
          <td><input type="text" value="<{$product.url}>" name="url" class="width-long"/></td>
          <td></td>
        </tr>
<tr>
          <td>商家链接</td>
          <td><input type="text" value="<{$product.shop_url}>" name="shop_url" class="width-long"/></td>
          <td></td>
        </tr>
<tr>
          <td>价格</td>
          <td><input type="text" value="<{$product.price}>" name="price"/></td>
          <td></td>
        </tr>
<tr>
          <td>折扣</td>
          <td><input type="text" value="<{if $product.discount}><{$product.discount}><{else}>100<{/if}>" name="discount" />%</td>
          <td></td>
        </tr>
     <tr>
          <td>颜色</td>
          <td>
<select name="tid[]" >
      	<option value="">选择...</option>
      	<{if $colors}>
      		<{foreach from=$colors item=t}>
      			<option value="<{$t.tid}>" <{if $t.tid==$color}>selected<{/if}> ><{$t.name}></option>
      		<{/foreach}>
      	<{/if}>
      </select>
</td>
          <td></td>
        </tr>
        <tr>
          <td>类别</td>
          <td>
	  <select name="tid[]" id="ptype_first">
      	<option value="">选择...</option>
      	<{if $ptype_firsts}>
      		<{foreach from=$ptype_firsts item=t}>
      			<option value="<{$t.tid}>" <{if $t.tid==$ptype_first}>selected<{/if}> ><{$t.name}></option>
      		<{/foreach}>
      	<{/if}>
      </select>
      
      &nbsp;&nbsp;&nbsp;&nbsp;
      <select name="tid[]" id="ptype_second">
      <{if $ptype_second_html}>
      <{$ptype_second_html}>
      <{else}>
      <option value="">选择...</option>
      <{/if}>
      </select>
      
       &nbsp;&nbsp;&nbsp;&nbsp;
      <select name="tid[]" id="ptype_third">
      <{if $ptype_third_html}>
      <{$ptype_third_html}>
      <{else}>
      <option value="">选择...</option>
      <{/if}>
      </select>
      </td>
          <td></td>
        </tr>
        <tr>
          <td>缩略图</td>
          <td>
          <input type="file" name="thumbnail"/>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a id="thumbnail_view" target="_blank" href="<{$product.images.thumbnail.path}>"><{$product.images.thumbnail.path}></a>
          </td>
          <td></td>
        </tr>
        <tr>
          <td>大图</td>
          <td>
          <input type="file" name="image"/>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a id="image_view" target="_blank" href="<{$product.images.image.path}>"><{$product.images.image.path}></a>
          </td>
          <td></td>
        </tr>
        <tr>
          <td>上传进度条</td>
          <td>
<div class="progress">
    <div class="bar"></div >
    <div class="percent">0%</div >
</div>
<style type="text/css">
.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
          </td>
          <td></td>
        </tr>
<tr>
          <td></td>
          <td><input type="submit"  name="submit" value="提交" /></td>
          <td></td>
        </tr>
       
      </table>
 </form>
 
<script type="text/javascript">
$("#ptype_first").change(function(){
	var parent = $(this).val();
	$.ajax({
		type: "POST",
		url: "admin.php?ctrl=taxonomy&act=get_child_terms_options_html",
		data: 'parent='+parent,
		timeout: 20000,
		error: function(){alert('error');},
		success: function(html){
		  $('#ptype_second').html('<option value="">选择...</option>'+html);
		}
	});
});
$("#ptype_second").change(function(){
	var parent = $(this).val();
	$.ajax({
		type: "POST",
		url: "admin.php?ctrl=taxonomy&act=get_child_terms_options_html",
		data: 'parent='+parent,
		timeout: 20000,
		error: function(){alert('error');},
		success: function(html){
		  $('#ptype_third').html('<option value="">选择...</option>'+html);
		}
	});
});
var bar = $('.bar');
var percent = $('.percent');
$('#form_product_edit').ajaxForm({
	beforeSend: function() {
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    complete: function(xhr) {
    	var data = JSON.parse(xhr.responseText);
    	var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);
        
        
    	var pid = data.pid;
    	if(pid > 0){
    		
    		$('#thumbnail_view').attr('href', data['thumbnail']);
            $('#thumbnail_view').html(data['thumbnail']);
            $('#image_view').attr('href', data['image']);
            $('#image_view').html(data['image']);
            
            if($('#show_pid input[name="pid"]').val()){
                alert('修改成功');
            }else{
                $('#show_pid input[name="pid"]').val(pid);
                $('#show_pid').show();
                alert('添加成功');
            }
        }else if(pid == '-1'){
            alert('添加产品失败');
        }else{
            alert('未知错误');
        }
    }
});

$('#get_data_remote').click(function(){
	
	var source_id = $('#source_id').val();
	var source = $('#source').val();
	if(!source_id){alert('源商品id不能为空');return false;}
	if(!source){alert('请选择商品来源');return false;}
	$('#get_data_remote_msg').html('正在获取数据...');
	$.ajax({
		type: "POST",
		url: "admin.php?ctrl=product&act=get_data_remote",
		data: 'source_id='+source_id+'&source='+source,
		timeout: 20000,
		error: function(){alert('error');},
		success: function(result){
		  $('#get_data_remote_msg').html('成功获取数据');
		  result = JSON.parse(result);
		  
		  for(k in result){
			$("#form_product_edit input[name='"+k+"']").attr('value', result[k]);
		  }
		  
		}
	});
});

</script>